package com.news;

import com.news.model.News;
import com.news.model.NewsList;
import com.news.views.NewsContentView;
import com.news.views.NewsHeaderView;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class NewsActivity extends Activity implements OnTouchListener { 
	
	private ScreenController sc;

	private NewsList news;
	
	private ViewFlipper mFlipper;
	
	private NewsHeaderView header;
	private NewsContentView body;
		
	float downXValue = 0;
	float downYValue = 0;
	

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_detail);
        
        
        
        mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper2));

        sc = (ScreenController) getApplication();
        
        news = sc.getNews();
        
        header  = (NewsHeaderView) findViewById(R.id.header_news_app);
        body = (NewsContentView)findViewById(R.id.body_news_app);
        
        setNewInfo();
        
        if(sc.getNews().getSizeNews() == 1){
        	header.setDisableButtons();        
        	
        } else {
        		
        		body.setOnTouchListener(this);
        	 	
        
        		header.getLeftButton().setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						header.getLeftButton().setBackgroundResource(R.drawable.previous_onclic);
						previous();
					
					}
		
		
				});
		        
        	 	header.getRightButton().setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						header.getRightButton().setBackgroundResource(R.drawable.next_onclic);
						next();
						try {
							wait(750);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						header.getRightButton().setBackgroundResource(R.drawable.next);
						
					}
				});   
        
        }
    }
    
    @Override
	public boolean onTouch(View v, MotionEvent event) {

		
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN :
				
				downXValue = event.getX();
                downYValue = event.getY();
                break;
                
			case MotionEvent.ACTION_UP :	
				
				 float currentX = event.getX();  
	             float currentY = event.getY(); 
	          
	             boolean diferenciaTotal = (Math.abs(downXValue - currentX) > Math.abs(downYValue - currentY));
	             
	             if((downXValue > currentX) &&
	            	(sc.getCurrent() < sc.getNews().getSizeNews()) &&
	            	(diferenciaTotal))
	             {
	            	 
	            	 next();
	            	 
	             }
	             
	             if((downXValue < currentX) &&
	             	((sc.getCurrent() > 0)) &&
	             	(diferenciaTotal))
	             {
	            	 previous();
	             }
	             break;
		}
		
		return true;
		
	}

	protected void next() {
		
		int size = sc.getNews().getSizeNews();
		int next = sc.getCurrent() + 1;
		
		if(next < size){

			mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right_anim));
			mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_to_left_anim));
			mFlipper.startFlipping();
			sc.nextNews();
			setNewInfo();
		
			mFlipper.stopFlipping();
			
			
			
			
			
			
		}
		
	}
	
	protected void previous(){
		
		int prev = sc.getCurrent() - 1;
		
		if(prev >= 0 ){
		
			mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_to_left_anim));
			mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right_anim));
			mFlipper.startFlipping();
			sc.prevNews();
			setNewInfo();
			
			mFlipper.stopFlipping();
			
			header.getLeftButton().setBackgroundResource(R.drawable.previous);
			
		}
	}
	
	private void setNewInfo(){
		News item = news.getNews().elementAt(sc.getCurrent());
		body.setInfo(item.getLink());
		
	}
   
    
 
}